#include<iostream>
#include<unistd.h>
#include<fcntl.h>

void SetNonBlock(int fd){//将fd变成非阻塞状态
    int flag=fcntl(fd,F_GETFL);
    if(flag==-1){
        std::cerr<<"fcntl error"<<std::endl;
        exit(1);
    }
    fcntl(fd,F_SETFL,flag | O_NONBLOCK);
}
int main(){
    SetNonBlock(0);
    char OutBuf[1024];
    while(true){
        std::cout<<"please enter#";
        fflush(stdout);
        sleep(1);
        int n=read(0,OutBuf,sizeof(OutBuf));
        if(n>0){
            OutBuf[n]=='\0';
            write(1,OutBuf,n);
        }
        else if(n==0){
            std::cout<<"EOF!"<<std::endl;
            break;
        }
        else{
            if(errno==EAGAIN||errno==EWOULDBLOCK){
                std::cout<<"未检测到数据"<<std::endl;
            }
            else{
                std::cout<<"发生read错误"<<std::endl;
                return 2;
            }
        }
    }
    return 0;
}